using System;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace Microsoft.Phone.Controls
{
	internal sealed class TransformAnimator
	{
		private static readonly PropertyPath TranslateXPropertyPath = new PropertyPath("X", new object[0]);
		private readonly Storyboard _sbRunning = new Storyboard();
		private readonly DoubleAnimation _daRunning = new DoubleAnimation();
		private TranslateTransform _transform;
		private Action _oneTimeAction;
		public double CurrentOffset
		{
			get
			{
				return this._transform.get_X();
			}
		}
		public TransformAnimator(TranslateTransform translateTransform)
		{
			this._transform = translateTransform;
			this._sbRunning.add_Completed(new EventHandler(this.OnCompleted));
			this._sbRunning.get_Children().Add(this._daRunning);
			Storyboard.SetTarget(this._daRunning, this._transform);
			Storyboard.SetTargetProperty(this._daRunning, TransformAnimator.TranslateXPropertyPath);
		}
		public void GoTo(double targetOffset, Duration duration)
		{
			this.GoTo(targetOffset, duration, null, null);
		}
		public void GoTo(double targetOffset, Duration duration, Action completionAction)
		{
			this.GoTo(targetOffset, duration, null, completionAction);
		}
		public void GoTo(double targetOffset, Duration duration, IEasingFunction easingFunction)
		{
			this.GoTo(targetOffset, duration, easingFunction, null);
		}
		public void GoTo(double targetOffset, Duration duration, IEasingFunction easingFunction, Action completionAction)
		{
			this._daRunning.set_To(new double?(targetOffset));
			this._daRunning.set_Duration(duration);
			this._daRunning.set_EasingFunction(easingFunction);
			this._sbRunning.Begin();
			this._sbRunning.SeekAlignedToLastTick(TimeSpan.Zero);
			this._oneTimeAction = completionAction;
		}
		public void UpdateEasingFunction(IEasingFunction ease)
		{
			if (this._daRunning != null && this._daRunning.get_EasingFunction() != ease)
			{
				this._daRunning.set_EasingFunction(ease);
			}
		}
		public void UpdateDuration(Duration duration)
		{
			if (this._daRunning != null)
			{
				this._daRunning.set_Duration(duration);
			}
		}
		private void OnCompleted(object sender, EventArgs e)
		{
			Action oneTimeAction = this._oneTimeAction;
			if (oneTimeAction != null && this._sbRunning.GetCurrentState() != null)
			{
				this._oneTimeAction = null;
				oneTimeAction.Invoke();
			}
		}
		public static void EnsureAnimator(FrameworkElement targetElement, ref TransformAnimator animator)
		{
			if (animator == null)
			{
				TranslateTransform translateTransform = TransformAnimator.GetTranslateTransform(targetElement);
				if (translateTransform != null)
				{
					animator = new TransformAnimator(translateTransform);
				}
			}
			if (animator == null)
			{
				throw new InvalidOperationException("The animation system could not be prepared for the target element.");
			}
		}
		public static TranslateTransform GetTranslateTransform(UIElement container)
		{
			if (container == null)
			{
				throw new ArgumentNullException("container");
			}
			TranslateTransform translateTransform = container.get_RenderTransform() as TranslateTransform;
			if (translateTransform == null)
			{
				if (container.get_RenderTransform() == null)
				{
					translateTransform = new TranslateTransform();
					container.set_RenderTransform(translateTransform);
				}
				else
				{
					if (container.get_RenderTransform() is TransformGroup)
					{
						TransformGroup transformGroup = container.get_RenderTransform() as TransformGroup;
						translateTransform = Enumerable.FirstOrDefault<TranslateTransform>(Enumerable.Select<Transform, TranslateTransform>(Enumerable.Where<Transform>(transformGroup.get_Children(), (Transform t) => t is TranslateTransform), (Transform t) => (TranslateTransform)t));
						if (translateTransform == null)
						{
							translateTransform = new TranslateTransform();
							transformGroup.get_Children().Add(translateTransform);
						}
					}
					else
					{
						TransformGroup transformGroup2 = new TransformGroup();
						Transform renderTransform = container.get_RenderTransform();
						container.set_RenderTransform(null);
						transformGroup2.get_Children().Add(renderTransform);
						translateTransform = new TranslateTransform();
						transformGroup2.get_Children().Add(translateTransform);
						container.set_RenderTransform(transformGroup2);
					}
				}
			}
			return translateTransform;
		}
	}
}
